<?php
function sanitize_output($buffer) {
	$s = array('/\>[^\S ]+/s', '/[^\S ]+\</s','/(\s)+/s');
	$r = array('>','<','\\1');
	$bb = preg_replace($s, $r, $buffer);
	return $bb;
}


function get_current_theme() {
	$data=new database;
	$data->db(DATABASE)->collection('settings');
	$data->criteria(array('key'=>'current_theme'));
	$data->cache(true)->cachekey(DATABASE.'.current_theme');
	$row=$data->findOne();
	$data->close();
	return $row;
}
function get_meta_data($id='') {
	$data=new database;
	$data->db(DATABASE)->collection('meta_data');
	$data->criteria(array('page_id'=>$id));
	//$data->cache(true)->cachekey(DATABASE.'meta_'.$id);
	$row=$data->findOne();
	$data->close();
	return $row;
}
function get_site_data() {
	$data=new database;
	$data->db(DATABASE)->collection('settings');
	$data->criteria(array('key'=>'site_information'));
	$data->cache(true)->cachekey(DATABASE.'.site');
	$row=$data->findOne();
	$data->close();
	return $row;
	
}

function get_navigation_parents() {
		$data=new database;
		$data->db(DATABASE)->collection('pages');
		$data->criteria(array('active'=>intval(1),'is_parent'=>intval(1)))->sort(array('order'=>1)); //$data->criteria(array('domain'=>array('$exists'=>true)));
		$data->cache(true)->cachekey(DATABASE.'.navigation');
		$rows=$data->findNl();
		//print_r($rows);
		foreach($rows['results'] as $row) {
			$selected=($row['url']==CURRENT_PAGE) ? true : false;
			$children=get_navigation_children($id);
			$output[]=array('id'=>(string)$row['_id'],'text'=>$row['text'],'url'=>$row['url'],'current'=>$selected,'children'=>$children);	
		}
return $output;
}
function get_navigation_children($parent='') {
		// current selected item
		$data=new database;
		$data->db(DATABASE)->collection('pages');
		$data->criteria(array('active'=>intval(1),'parent'=>$parent))->sort(array('order'=>1)); //$data->criteria(array('domain'=>array('$exists'=>true)));
		$data->cache(true)->cachekey(DATABASE.'.navigation_children');
		$rows=$data->findNl();
		$data->close();
		foreach($rows['results'] as $row) {
			// putput
			$selected=($row['slug']==$slug) ? true : false;
			$output[]=array('id'=>(string)$row['_id'],'text'=>$row['text'],'url'=>$row['url'],'current'=>$selected);	
		}
return $output;	
}
function get_content($_id) {
	// singular piece of content.
		$data=new database;
		$data->db(DATABASE)->collection('content');
		$data->criteria(array('_id'=>new MongoId($_id)))->sort(array('order'=>1));
		//$data->cache(true)->cachekey(DATABASE.'.content_'.$_id); // should cache all the rows
		$rows=$data->findOne();
		return $rows;
}
function get_all_content($page_id) {
		
		$data=new database;
		$data->db(DATABASE)->collection('content');
		$data->criteria(array('page_id'=>$page_id))->sort(array('order'=>1));
	//	$data->cache(true)->cachekey(DATABASE.'.page_content_'.$page_id); // should cache all the rows // sort this out for the future...
		$rows=$data->findNl();
		return $rows;
}
function get_pages() {
		$slug=(empty($slug)) ? '/' : $slug;
		$data=new database;
		$data->db(DATABASE)->collection('pages');
		$data->criteria(array('is_parent'=>intval(1)))->sort(array('order'=>1)); //$data->criteria(array('domain'=>array('$exists'=>true)));
		//$data->cache(true)->cachekey(DATABASE.'.navigation');
		$rows=$data->findNl();
		
		foreach($rows['results'] as $row) {
			// putput
		
			$selected=($row['url']==$slug) ? true : false;
			$children=get_navigation_children((string)$row['_id']);
			$output[]=array('id'=>(string)$row['_id'],'text'=>$row['text'],'url'=>$row['url'],'current'=>$selected,'children'=>$children);	
		}
		// work out children later and tack on as array on the end
return $output;
}
function get_page_id($url='') {
	$url=(empty($url)) ? '/' : $url;
	
	$data=new database;
	$data->db(DATABASE)->collection('pages');
	$data->criteria(array('url'=>$url));
	$row=$data->findOne();
	$data->close();
	return $row;	
}
function get_page($id='',$url='') {
	$data=new database;
	$data->db(DATABASE)->collection('pages');
	$data->criteria(array('_id'=>new MongoId($id)));
	$row=$data->findOne();
	$data->close();
	return $row;
}
function add_content($page_id) {
	$data=new database;
	$data->db(DATABASE)->collection('content');
	$item=array('page_id'=>$page_id,
				'order'=>intval(100),
				'content'=>'Please enter some content',
				'active'=>intval(0),
				'type'=>'text'
				);
	$data->criteria($item)->cache(true)->cachekey(DATABASE.'.page_content_'.$page_id);
	$item=$data->insert();
	return $item;			
}
function truncate($str='',$len=300) {
	return substr($str,0,$len).'...';
}
function get_parent_pages_as_opts($input='') {
		$data=new database;
		$data->db(DATABASE)->collection('pages');
		$data->criteria(array('is_parent'=>intval(1)));
		$rows=$data->findAll();
		$output[]='<option value="">No Parent</option>';
		foreach($rows['results'] as $row) {
			$selected=((string)$row['_id']==$input) ? ' selected="selected"' : '' ;
			$output[]='<option value="'.(string)$row['_id'].'"'.$selected.'>'.$row['text'].'</option>';	
			
		}
return implode("\r\n",$output);
}
function add_page($item) {
		$data=new database;
		$data->db(DATABASE)->collection('pages');
		$data->criteria($item)->expireCache(DATABASE.'.navigation');
		$page=$data->insert(); // what about the cache ?
		// insert the content too - takes care of it all
		$data=new database;
		$data->db(DATABASE)->collection('content');
		$data->criteria(array('page_id'=>(string)$page['_id'],'type'=>'text','active'=>intval(0),'content'=>'Page under construction'));
		//$data->cache(true)->cachekey(DATABASE.'.page_content_'.(string)$page['_id']); // should cache all the rows
		$content=$data->insert();
		$output['page']=$page;
		$output['content']=$content;
		return $output; // this returns the whole item	
}
function expireCache($cachekey) {
			$mc = new Memcached();
			$mc->addServer('localhost', 11211);	
			$mc->delete($cachekey);
}
function updateContent($arr,$page_id) {
	/*Move this to the admin class soon*/
	$data=new database();
	$data->db(DATABASE)->collection('content');
		foreach($arr as $key=>$val) {
			$d['active']=intval($val['active']);
			$d['order']=intval($val['order']);
			$id=$val['id'];
			if(!empty($val['content'])) {
				$d['content']=$val['content'];	
			}
		
		$newData=array('$set'=>$d);
		$data->criteria($newData);
		$data->update($id);
		unset($d);
		}
	$data->close();
}
function delete_content($id) {
	$data=new database();
	$data->db(DATABASE)->collection('content');
	$data->criteria(array('_id'=>new MongoId($id)));
	$data->remove()->close();
}
function getPageInformation($id) {
			$data=new database();
			$data->db(DATABASE)->collection('pages');
			$data->criteria(array('_id'=>new MongoId($id)));
			$data->cache(true)->cachetime(10)->cachekey(DATABASE.'page_information_'.$id);
			return $data->findOne();	
		}
function getContentSnippet($id) {
			$data=new database();
			$data->db(DATABASE)->collection('content');
			$data->criteria(array('_id'=>new MongoId($id)));
			$data->cache(true)->cachetime(10)->cachekey(DATABASE.'content_snippet_'.$id); // expire these
			return $data->findOne();	
		}
		
?>